package service

import (
	"context"
	"iris-wms/apps/admin/web/dto/din"
	"iris-wms/apps/admin/web/dto/dout"
	"iris-wms/common/pkg/db"
	"iris-wms/common/pkg/db/dao"
)

var _ AdminLogService = (*adminLogService)(nil)

type AdminLogService interface {
	DeleteByIdList([]uint) error
	GetTable(din.LogIn) ([]*dout.AdminLogOut, int64, error)
}

func NewAdminLogService() *adminLogService {
	return &adminLogService{}
}

type adminLogService struct{}

func (impl *adminLogService) DeleteByIdList(idList []uint) (err error) {
	q := dao.Use(db.GDB1).AdminLogOut
	_, err = q.Where(q.ID.In(idList...)).Delete()
	return
}

func (impl *adminLogService) GetTable(in din.LogIn) (rows []*dout.AdminLogOut, total int64, err error) {
	q := dao.Use(db.GDB1).AdminLogOut
	do := q.WithContext(context.Background())

	if in.UserId > 0 {
		do = do.Where(q.Uid.Eq(in.UserId))
	}
	if in.GroupName != "" {
		do = do.Where(q.GroupName.Eq(in.GroupName))
	}
	if in.Name != "" {
		do = do.Where(q.Name.Eq(in.Name))
	}

	rows, total, err = do.FindByPage(in.Offset(), in.PageSize)
	return
}
